package com.umerclass.ui.user;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.king.app.updater.AppUpdater;
import com.king.app.updater.callback.AppUpdateCallback;
import com.umerclass.R;
import com.umerclass.bean.home.ItemOneBean;
import com.umerclass.frame.model.InfoResult;
import com.umerclass.frame.ui.fragment.BaseFragment;
import com.umerclass.frame.ui.recycleview.listener.OptListener;
import com.umerclass.ui.home.adapter.ItemAdapterTwo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;

public class UserFragment extends BaseFragment implements OptListener {

    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.linear)
    LinearLayout linear;
    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    @BindView(R.id.linear_login)
    LinearLayout linearLogin;
    @BindView(R.id.linear_help)
    LinearLayout linearHelp;
    @BindView(R.id.linear_update)
    LinearLayout linearUpdate;
    private ItemAdapterTwo itemAdapterOne;
    private RequestManager glideRequestManager;
    private GridLayoutManager gridLayoutManager;
    private String call = "";
    private String mUrl = "";//升级apk地址
    private AppUpdater mAppUpdater;

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_user;
    }

    @Override
    public void init() {
        glideRequestManager = Glide.with(this);
        gridLayoutManager = new GridLayoutManager(getActivity(), 4);
        recycleview.setLayoutManager(gridLayoutManager);
        todo();
    }

    @Override
    public void loadData() {

    }

    @OnClick({R.id.linear_login, R.id.linear_help, R.id.linear_update})
    public void OnClick(View view) {
        switch (view.getId()) {
            //密码登录的
            case R.id.linear_login:
                //判断用户是否登录
                switchTo(getActivity(), UserLoginActivity.class, false);
                //switchTo(getActivity(), UserCenterActivity.class, false);
                break;
            case R.id.linear_help:
                PermissionGen.needPermission(this, 100,
                        new String[]{
                                Manifest.permission.CALL_PHONE
                        });
                break;
            //版本升级
            case R.id.linear_update:
                break;
            default:
                break;
        }
    }

    @Override
    public void onOptClick(View view, Object object) {

        switch (view.getId()) {
            case R.id.linear:
                int position = (int) object;
                if (position == 0) {
                    switchTo(getActivity(), MyDingYueActivity.class, false);
                } else if (position == 1) {
                    switchTo(getActivity(), MyBaoMingActivity.class, false);
                } else if (position == 2) {
                    switchTo(getActivity(), MyShouChangActivity.class, false);
                } else if (position == 3) {
                    switchTo(getActivity(), MyDuiHuanActivity.class, false);
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    private void todo() {
        List<ItemOneBean> infos2 = new ArrayList<>();
        ItemOneBean itemOneBean = new ItemOneBean();
        itemOneBean.setName("我的订阅");

        ItemOneBean itemOneBean2 = new ItemOneBean();
        itemOneBean2.setName("我的报名");

        ItemOneBean itemOneBean3 = new ItemOneBean();
        itemOneBean3.setName("我的收藏");

        ItemOneBean itemOneBean4 = new ItemOneBean();
        itemOneBean4.setName("兑换码");

        infos2.add(itemOneBean);
        infos2.add(itemOneBean2);
        infos2.add(itemOneBean3);
        infos2.add(itemOneBean4);
        itemAdapterOne = new ItemAdapterTwo(getActivity(), infos2, R.layout.item_home_one, glideRequestManager, this);
        recycleview.setAdapter(itemAdapterOne);
    }

    @PermissionSuccess(requestCode = 100)
    public void doSomething() {
        dialogPhone();
    }

    private void dialogPhone() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("确认呼叫？");
        builder.setTitle("提示");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + call));
                startActivity(intent);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    /**
     * 简单一键后台升级
     */
    private void click1(){
        mAppUpdater = new AppUpdater(getContext(),mUrl);
        mAppUpdater.start();
    }

    /**
     * 系统弹框升级
     */
    private void click2(){
        new AlertDialog.Builder(getActivity())
                .setTitle("发现新版本")
                .setMessage("1、新增某某功能、\n2、修改某某问题、\n3、优化某某BUG、")
                .setPositiveButton("升级", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mAppUpdater = new AppUpdater.Builder()
                                .serUrl(mUrl)
                                .build(getContext())
                                .setUpdateCallback(new AppUpdateCallback() {
                                    @Override
                                    public void onProgress(long progress, long total, boolean isChange) {

                                    }

                                    @Override
                                    public void onFinish(File file) {
                                        showToast("下载完成");
                                    }
                                });
                        mAppUpdater.start();
                    }
                }).show();
    }
}
